﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace projetoFinal
{
    public class operaModulo
    {
        public static List<modulo> listar(string field, string value, int crit)
        {
            List<modulo> listModulo = new List<modulo>();
            string strSQL = string.Empty;

            if (field.Equals(""))
            {
                strSQL = " SELECT * FROM modulo";
            }
            else
            {
                strSQL = DAOHelper.sqlQueryBuilder("modulo", field, value, crit);
            }

            using (IDataReader dr = DAOHelper.executeQuery(strSQL))
            {
                while (dr.Read())
                {
                    modulo objModulo = new modulo(Int64.Parse(dr["codModulo"].ToString()), dr["descr"].ToString(), (status) Int32.Parse(dr["status"].ToString()));
                    listModulo.Add(objModulo);
                }
            }
            return listModulo;
        }
        public static int incluir(modulo objModulo)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" INSERT INTO ");
            strSQL.Append(" modulo ");
            strSQL.Append(" ( ");
            strSQL.Append("     descr, status ");
            strSQL.Append(" ) ");
            strSQL.Append(" VALUES ");
            strSQL.Append(" ( ");
            strSQL.AppendFormat(" {0} ", "'" + objModulo.Descr + "'");
            strSQL.AppendFormat(" , {0} ", "'" + objModulo.Status + "'");
            strSQL.Append("  ); ");
            return DAOHelper.executeDML(strSQL.ToString());
        }
        public static int apagar(Int64 codModulo)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" DELETE FROM modulo ");
            strSQL.Append(" WHERE ");
            strSQL.Append(" codModulo = ");
            strSQL.AppendFormat("{0}; ", codModulo);
            return DAOHelper.executeDML(strSQL.ToString());
        }
        public static int editar(modulo objModulo)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" UPDATE modulo ");
            strSQL.Append(" SET ");
            strSQL.AppendFormat(" descr = {0} ", "'" + objModulo.Descr + "'");
            strSQL.AppendFormat(" status = {0} ", "'" + (Int32)objModulo.Status + "'");
            strSQL.Append(" WHERE ");
            strSQL.AppendFormat(" codModulo = {0}; ", objModulo.CodModulo);
            return DAOHelper.executeDML(strSQL.ToString());
        }
        public static modulo buscar(Int64 codModulo)
        {
            modulo objModulo;
            List<modulo> listaModulo = listar(" codModulo ", codModulo.ToString(), 1);
            if (listaModulo.Count == 1)
            {
                objModulo = listaModulo[0];
            }
            else
            {
                objModulo = null;
            }
            return objModulo;
        }
        public static modulo buscar(string modulo)
        {
            modulo objModulo = null;

            string strSQL = DAOHelper.sqlQueryBuilder("modulo", " upper(descr) ", modulo.ToString().ToUpper(), 1);
            using (IDataReader dr = DAOHelper.executeQuery(strSQL))
            {
                if (dr.Read())
                {
                    objModulo = new modulo(Int64.Parse(dr["codModulo"].ToString()), dr["descr"].ToString(), (status)Int32.Parse(dr["status"].ToString()));
                }
            }

            return objModulo;
        }
    }
}
